package minov.test;

import java.util.Arrays;

import minov.parser.ArgumentDefinitionException;
import minov.parser.ArgumentParser;
import minov.parser.IntegerArgument;
import minov.parser.SwitchArgument;
import minov.parser.ValidationResult;

public class SimpleParse {

   private static void ParseArguments(String[] arguments)
         throws ArgumentDefinitionException {

      printTestsSeparator(arguments);
      // initialize
      ArgumentParser parser = new ArgumentParser();
      
      // add switch argument
      SwitchArgument version = new SwitchArgument("v", "version");
      parser.addArgument("version", version);
      
      // add integer argument with boundaries 0-5 and default valie 0
      IntegerArgument logLevel = new IntegerArgument("l", "log", 0, 5, 0);
      parser.addArgument("loglevel", logLevel);

      // result of parsing, if all arguments are valid
      ValidationResult result = null;
      try {
         result = parser.parseArguments(arguments);
      } catch (ArgumentDefinitionException exception) {
         System.out.println("Exception: "+exception.getMessage());
         return;
      }

      if (result.isValid() == false) {
         System.out.println(result.errorMessage());
      }

      if (version.isEntered()) {
         System.out.println("Application version : 1.0.1");
      }

      if (logLevel.isEntered()) {
         System.out.println("Log level: " + logLevel.getValue());
      }

   }

   private static void printTestsSeparator(String[] arguments)
   {
      System.out.println("=========================");
      System.out.println("Parsing arguments: "+Arrays.toString(arguments));
   }
   
   public static void main(String[] arguments) {

      String[] simpleArguments =  {};
      ParseArguments(simpleArguments);

      String[] simpleArguments2 =  {"-v"};
      ParseArguments(simpleArguments2);
            
      String[] simpleArguments3 =  {"-l=1"};
      ParseArguments(simpleArguments3);

      String[] simpleArguments4 =  {"-l","3"};
      ParseArguments(simpleArguments4);

      String[] simpleArguments5 =  {"X"};
      ParseArguments(simpleArguments5);

      String[] simpleArguments6 =  {"-l","8"};
      ParseArguments(simpleArguments6);
   }
}
